﻿using FMStudio.Documents.ProjectDocument.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;

namespace FMStudio.Documents.ProjectDocument
{
    [Export(typeof(IDocumentType)), Export]
    public class ProjectDocumentType : DocumentType<ProjectDocument>
    {
        private const string FormatVersion = "FormatVersion";


        public ProjectDocumentType()
            : base(Resources.DocumentDescription, Resources.DocumentExtension)
        {
        }


        #region Methods
        #region Public Methods
        public override bool CanNew() { return true; }

        public override bool CanOpen() { return true; }
        #endregion

        #region Protected Methods
        protected override ProjectDocument NewCore(string filePath)
        {
            ProjectDocument document = new ProjectDocument();
            document.Properties.Name = Path.GetFileNameWithoutExtension(filePath);

            return document;
        }

        protected override ProjectDocument OpenCore(string filePath)
        {
            ProjectDocument document = new ProjectDocument();

            try
            {
                XElement root = XDocument.Load(filePath).Root;

                int formatVersion = int.Parse(root.Attribute(FormatVersion).Value);
                if (formatVersion == 1)
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(ProjectDocument));

                    using (StreamReader sr = new StreamReader(filePath))
                    {
                        document = serializer.Deserialize(sr) as ProjectDocument;
                    }
                }
                else if (formatVersion > 1)
                {
                    throw new NotSupportedException(Resources.LaterVersion);
                }
                else
                {
                    throw new FileLoadException(Resources.UnknownVersoin);
                }
            }
            catch
            {
                throw new FileLoadException(Resources.UnknownType);
            }

            return document;
        }

        protected override bool CanSaveCore() { return true; }

        protected override void SaveCore(ProjectDocument document, string filePath)
        {
            if (filePath.CompareTo(document.FilePath) != 0)
            {
                throw new NotSupportedException(Resources.CannotMove);
            }

            XmlSerializerNamespaces xns = new XmlSerializerNamespaces();
            xns.Add(string.Empty, string.Empty);
            XmlSerializer serializer = new XmlSerializer(typeof(ProjectDocument));

            using (StreamWriter sw = new StreamWriter(filePath))
            {
                serializer.Serialize(sw, document, xns);
            }
        }
        #endregion
        #endregion
    }
}
